<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>
<%@page import="com.kcs.common.utils.commonUtils"%>
<%@page import="com.kcs.common.utils.PropsUtils"%>
<%@page import="com.kcs.common.utils.Constant"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Inquiry Margin A/C Summary</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="jsp/script/jquery/plugin/fancybox/jquery.easing-1.3.pack.js"></script>
<script type="text/javascript" src="jsp/script/jquery/plugin/fancybox/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="jsp/script/jquery/plugin/fancybox/jquery.fancybox-1.3.4.pack.js"></script>   
<script type="text/javascript" src="jsp/script/jsDatePick.full.1.3.js"></script>   	
<script type="text/javascript" src="jsp/script/common.js"></script>  
<link rel="stylesheet" type="text/css" href="jsp/script/jquery/plugin/fancybox/jquery.fancybox-1.3.4.css"/>
<link rel="stylesheet" type="text/css" href="jsp/css/table.css"/>
<link rel="stylesheet" type="text/css" href="jsp/css/jsDatePick_ltr.css" />
<link rel="stylesheet" type="text/css" href="jsp/css/fxmarginStyle.css"/>
<script type="text/javascript">
	var g_data_size = 0;
	
	$(document).ready(function() {
		jQuery("#action").val("init");
		jQuery("#cashFlag").val(false);
		
		new JsDatePick({
			useMode:2,
			target:"valueDate",
			dateFormat:"%d/%m/%Y"
		});

		if(jQuery("#valueDate").val() == "") jQuery("#valueDate").val(getCurrentDate());

		var callMargin = jQuery("#marginCall").val();

		jQuery("#custLimit").val(addDigit(jQuery("#custLimit").val()));
		jQuery("#cashBal").val(addDigit(jQuery("#cashBal").val()));
		jQuery("#realizedPl").val(addDigit(jQuery("#realizedPl").val()));
		jQuery("#unrealizedPl").val(addDigit(jQuery("#unrealizedPl").val()));
		jQuery("#equityBal").val(addDigit(jQuery("#equityBal").val()));
		jQuery("#initialMargin").val(addDigit(jQuery("#initialMargin").val()));
		jQuery("#excessAmt").val(addDigit(jQuery("#excessAmt").val()));
		jQuery("#mtnMargin").val(addDigit(jQuery("#mtnMargin").val()));
		jQuery("#marginCall").val(addDigit(jQuery("#marginCall").val()));

		if(callMargin > 0 ){
			jQuery("#call_margin_td").css('color','red');
			jQuery("#marginCall").css('color','red');
		}

		var dataNotFound = jQuery("#dataNotFound").val();

		if(dataNotFound == 'true'){
			alert(m_no_data_found);
		}

		setMarginAccNoFormat("tfMarginAccNo","marginAccNo");
	});

	function addDigit(i_value){
		var result;
		try{
			result = parseFloat(i_value).toFixed(2);
			result = addCommas(result);
		} catch(err){
			console.log('error pares float [' + i_value + ']');
		}
		

		return result;
	}

	function saveFunction(){
		var objData = new Array();
		var settleAmount;
		var settleRemain;
		var dealsSettleId;
		var rowVersion;
		var dealId;

		g_data_size = jQuery("[name=settleAmount]").length;
		
		for(var i = 1 ; i <= g_data_size; i++){
			settleAmount 	= jQuery("#settleAmount" + i).val();
			settleRemain 	= jQuery("#settleRemain" + i).val();
			dealsSettleId 	= jQuery("#dealsSettleId" + i).val();
			rowVersion 		= jQuery("#rowVersion" + i).val();
			dealId 			= jQuery("#dealId" + i).val();

			if(settleAmount == null || settleAmount == ""){
				 alert("Please input settleAmount");
				 return;
			}

			objData.push({
				  "settleAmount" 	: settleAmount, 
				  "settleRemain" 	: settleRemain, 
				  "dealsSettleId" 	: dealsSettleId, 
				  "rowVersion" 		: rowVersion,
				  "dealId"			: dealId
				  });
		}
		
		loading();
		jQuery.post("/FxMargin/ValidateSettlementServlet",{
			"objData" : JSON.stringify(objData),
			},
			function(data){
				if(data != null && data.data == "s"){
					unloading();
					alert(m_save_success);
					search();
				}else{
					unloading();
					alert(m_save_fail);
				}
				$.fancybox.close();
				},"json").error(
			function(){
				unloading();
				alert("Ajax error");
			}
		);
		
	}
			
	function resetFunction(){
		jQuery("#custLimit").attr('disabled','disabled');
		jQuery("#cashBal").attr('disabled','disabled');
		jQuery("#realizedPl").attr('disabled','disabled');
		jQuery("#unrealizedPl").attr('disabled','disabled');
		jQuery("#equityBal").attr('disabled','disabled');
		jQuery("#initialMargin").attr('disabled','disabled');
		jQuery("#excessAmt").attr('disabled','disabled');
		jQuery("#mtnMargin").attr('disabled','disabled');
		jQuery("#marginCall").attr('disabled','disabled');
		
		loading();
		jQuery("#action").val("init");
		submitform();
	}
	
	function search(){
		updateMarginAccNoValue("tfMarginAccNo","marginAccNo");
		
		var cifNo 		= jQuery("#cifNo").val();
		var valueDate 	= jQuery("#valueDate").val();

		if(cifNo == "") 	{ alert("Please fill required data in complete"); return;}
		if(valueDate == "") { alert("Please fill required data in complete"); return;}

		var currentDate = toDate(getCurrentDate());
		var isDate		= toDate(valueDate);

		if(isDate.getTime() == currentDate.getTime()) jQuery("#cashFlag").val(true); 
		else if(isDate.getTime() > currentDate.getTime()) {
			jQuery("#custLimit").val('0.00');
			jQuery("#cashBal").val('0.00');
			jQuery("#realizedPl").val('0.00');
			jQuery("#unrealizedPl").val('0.00');
			jQuery("#equityBal").val('0.00');
			jQuery("#initialMargin").val('0.00');
			jQuery("#excessAmt").val('0.00');
			jQuery("#mtnMargin").val('0.00');
			jQuery("#marginCall").val('0.00');
			return;
		}

		jQuery("#custLimit").attr('disabled','disabled');
		jQuery("#cashBal").attr('disabled','disabled');
		jQuery("#realizedPl").attr('disabled','disabled');
		jQuery("#unrealizedPl").attr('disabled','disabled');
		jQuery("#equityBal").attr('disabled','disabled');
		jQuery("#initialMargin").attr('disabled','disabled');
		jQuery("#excessAmt").attr('disabled','disabled');
		jQuery("#mtnMargin").attr('disabled','disabled');
		jQuery("#marginCall").attr('disabled','disabled');

		loading();
		jQuery("#action").val("search");
		submitform();
	}	
	
	function changeSettleAmount(value, amount, index){
	
		if(value > amount){
			jQuery("#settleAmount" + index).val(0);
			jQuery("#settleRemain" + index).val(0);
		} else {
			jQuery("#settleRemain" + index).val(amount - value);
		}
	}

	// for popup customer //
	
	function onPopup(id, src, width, height){
		g_datacustomer = null;
		jQuery("#" + id + "_iframe").html("");
		src = "http://" + window.location.host + "/FxMargin/" + src + ".do?pAction=init";
		var html = "<center><iframe src='" + src +"' height='" + height +"px' width='" + width + "px' id='myIframe'></iframe></center>";

		jQuery("#" + id + "_iframe").html(html);

		openPopup(id);
		
	}

	function select(data){
		g_datacustomer = data;
	}
	
	function onCustomer(){
		/*	global variable 'g_datacustomer' is use	*/
 		
 		if(g_datacustomer != null ) {
			jQuery("#cifNo").val(g_datacustomer.cifNo);
			jQuery("#customerName").val(g_datacustomer.customerName);	
			jQuery("#tfMarginAccNo").val(g_datacustomer.marginAccNo);
			jQuery("#marginAccNo").val(g_datacustomer.marginAccNo);
			jQuery("#marginAccName").val(g_datacustomer.marginAccName);	
			jQuery.fancybox.close();
 		}
	}
	
	function offCustomer(){
		jQuery.fancybox.close();
	}
	// for popup customer //

	function setMarginAccNoFormat(strTf,strHd){
		var tfMarginAccNo = document.getElementById(strTf);
		var marginAccNo = document.getElementById(strHd);
		var tmp = marginAccNo.value;
		var result = null;
		
		if(tmp != ""){
			tmp = marginAccNo.value.replace(/-/g,"");
			tmp = lpad(tmp,10,"0");

			result = "";
			for(var i=0; i<tmp.length; i++){
				if(i==3 || i==4 || i==9){
					result += "-" +tmp.charAt(i);
				}else{
					result += tmp.charAt(i);
				}
			}
		
		}else{
			result = "";
		}
		
		tfMarginAccNo.value = result;
	}

 	function updateMarginAccNoValue(strTf,strHd){
		var tfMarginAccNo = document.getElementById(strTf);
		var marginAccNo = document.getElementById(strHd);
		var result = tfMarginAccNo.value.replace(/-/g,"");

		if(!isNaN(result) && result != ""){
			result = parseInt(result,10);
		}else{
			result = "";
		}
		marginAccNo.value = result;
	}
</script>
</head>
<body>

<%@ include	file="include/headerMenu.jsp"%>

<center>
	<div style="width:95%" align="center">
		<html:form action="/InquiryMarginAction.do">
			<html:hidden property="action" styleId="action"/>
			<html:hidden property="cashFlag" styleId="cashFlag"/>
			<html:hidden property="dataNotFound" styleId="dataNotFound"/>
			
			<table width="100%" class="MarginHeader">
				<tr>
					<td align="left">Inquiry Margin A/C Summary</td>
				</tr>
			</table>
			
			<div class="CriteriaBox">
				<table width="100%" class="MarginCriteria">
					<tr>
						<td align="right"><font color="red">* </font>CIF No. :</td>
						<td align="left" >
							<html:text property="cifNo" styleId="cifNo" readonly="true" styleClass="disabled"></html:text>
						</td>
						<td align="right">Customer Name :</td>
						<td align="left" >
							<html:text property="customerName" styleId="customerName" size="50" readonly="true" styleClass="disabled"></html:text>
						</td>
						<td align="left" colspan="2">
							<img src="jsp/images/btn/glass_search.gif" style="width:30px;cursor:pointer;" onclick="onPopup('popupCustomer','PopupCustomerAction',700,540);" />	
						</td>
					</tr>	
					<tr>
						<td align="right">Margin A/C No. :</td>
						<td align="left" >
							<input type="text" name="tfMarginAccNo" id="tfMarginAccNo" class="disabled" readonly/>
							<html:hidden property="marginAccNo" styleId="marginAccNo" />
						</td>
						<td align="right">Margin A/C Name :</td>
						<td align="left" ><html:text property="marginAccName" styleId="marginAccName" size="50" readonly="true" styleClass="disabled"></html:text></td>
						<td align="right"><font color="red">* </font>Date :</td>
						<td align="left" ><html:text property="date" styleId="valueDate" readonly="true"></html:text></td>
					</tr>
				</table>
				
				<table width="100%" class="MarginCriteria">
					<tr>
						<td width="100%" align="center">
							<input type="button" class="button" value="Search" onclick="search();"/>
							&nbsp;&nbsp;
							<input type="button" class="button" value="Reset" onclick="resetFunction();"/>
						</td>
					</tr>
				</table>
			</div>
				
			<div class="MarginTable" style="width: 40%;">
				<table align="center">
					<tr>
						<th width="100%" align="left" style="text-align: left;" colspan="2">Margin</th>
					</tr>
					<tr>
						<td width="50%" align="left" >Customer Limit (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.custLimit" styleId="custLimit"  styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Cash Balance (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.cashBal" styleId="cashBal" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Realized P/L (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.realizedPl" styleId="realizedPl" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Unrealized P/L (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.unrealizedPl" styleId="unrealizedPl" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Equity Balance (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.equityBal" styleId="equityBal" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Initial Margin (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.initialMargin" styleId="initialMargin" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Excess Amount (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.excessAmt" styleId="excessAmt" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" >Maintenance Margin (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.mtnMargin" styleId="mtnMargin" styleClass="disabled-right"></html:text></td>
					</tr>
					<tr>
						<td width="50%" align="left" id="call_margin_td">Call Margin (THB)</td>
						<td width="50%" align="right"><html:text property="dealsSum.marginCall" styleId="marginCall" styleClass="disabled-right"></html:text></td>
					</tr>
				</table>
			</div>
			
			<div style="display:none">
		        <div id="popupCustomer" style="width:710px;height: 600px">
		        	<div id="popupCustomer_iframe"></div>
					<table width="100%">
						<tr>
							<td width="33%" align="center">
								<input type="button" value="Ok" class="button" onclick="onCustomer();"/>
							</td>
							<td width="34%" align="center">
								<input type="button" value="Close" class="button" onclick="offCustomer();"/>
							</td>
						</tr>
					</table>
		        </div>
		    </div>
			<%@ include	file="include/footer.jsp"%> 
			<%@ include	file="include/message.jsp"%> 
			
		</html:form>	
		
	</div>
</center>

</body>
</html>